<?php
namespace Common\Controller;

/**
 * 前台基类Controller
 */
class HomeBaseController extends BaseController{
    /**
     * 初始化方法
     */
    public function _initialize(){
        parent::_initialize();
        // 判断博客是否关闭
        if(C('WEB_STATUS')!=1){
            $this->display('Public/web_close');
            exit();
        }
        // 组合置顶推荐where
        $recommend_map=array(
            'is_show'=>1,
            'is_delete'=>0,
            'is_top'=>1
            );
        // 获取置顶推荐学校
        $recommend=M('Article')
            ->field('aid,title')
            ->where($recommend_map)
            ->order('aid desc')
            ->select();
        // 获取最新评论
        $new_comment=D('Comment')->getNewComment();
        // 判断是否显示友情链接
        $show_link=MODULE_NAME.'/'.CONTROLLER_NAME.'/'.ACTION_NAME=='Home/Index/index' ? 1 : 0;
        // 获取地区信息
        if(!empty($_GET['province'])){
            $location=$_GET['province'];
        }else{
            $location='';
        }
        if(!empty($_GET['city'])){
            $location=$location."|".$_GET['city'];
        }
        if(!empty($_GET['area'])){
            $location=$location."|".$_GET['area'];
        }
        // 获取学校总数
        $condition['is_delete']=array('eq',0);
        $condition['location']=array('like',$location.'%');
        $all=D('Article')->where($condition)->count();
        // 分配常用数据
        $assign=array(
            'categorys'=>D('Category')->getAllData(),
            'tags'=>D('Tag')->getAllData($location),
            'links'=>D('Link')->getDataByState(0,1),
            'recommend'=>$recommend,
            'new_comment'=>$new_comment,
            'show_link'=>$show_link,
            'all'=>$all

            );

        // 获取用户信息
        if(!empty($_SESSION['user'])){
            $mid=$_SESSION['user']['uid'];
            $user=D('User')->where(array('uid'=>$mid))->find();
            $this->assign('user',$user);

        }else{
            $mid=0;
        }

        $data=D('Option')->select();
        foreach($data as $v){
            $value=explode(',',$v['value']);
            $para_name=$v['para_name'];
            $option[$para_name]=$value;
        }

        $this->assign('option',$option);

        $this->assign('mid',$mid);
        $this->assign($assign);
    }

    public function check_login(){
        if(empty($_SESSION['user'])){
            $this->error('您没有登录，操作失败');
        }
    }

    public function check_admin(){
        $this->check_login();
        $is_admin=D('User')->where(array('uid'=>$_SESSION['user']['uid']))->getField('is_admin');
        if($is_admin == 0){
            $this->error('您不是管理员，操作失败');
        }
    }

}

